﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using CPETwit.WPFClient.Business.Service;
using CPETwit.WPFClient.Business.TwitterServiceReference;
using CPETwit.WPFClient.Helper;

namespace CPETwit.WPFClient.Common
{
    public static class CommonData
    {
        public static User LOGGED_USER;

        public enum SexEnum
        {
            M, F
        }

        public static WCFServiceCaller GetServiceCaller()
        {
            App a;
            if (App.IsInDesignMode() == false)
                return new WCFServiceCaller();
            return null;
        }

        public static int GetFollowersCount(User u)
        {
            return u.AbonnementFollowerProperty != null
                       ? u.AbonnementFollowerProperty.Count() - 1
                       : 0;
        }

        public static int GetPublishersCount(User u)
        {
            return u.AbonnementPublisherProperty != null
                       ? u.AbonnementPublisherProperty.Count() - 1
                       : 0;
        }

        public static int GetTweetCount(User u)
        {
            return u.TweetTwitterProperty != null
                       ? u.TweetTwitterProperty.Count()
                       : 0;
        }

        public static bool IsFollowed(User follower, User publisher)
        {
            if (follower == null) throw new ArgumentNullException("follower");
            if (publisher == null) throw new ArgumentNullException("publisher");

            return (from ab in follower.AbonnementFollowerProperty
                    where ab.publisher == publisher.id
                    select ab).Count() != 0;
        }

        public static bool HasReTweeted(User user, Tweet tweet)
        {
            if (user == null) throw new ArgumentNullException("user");
            if (tweet == null) throw new ArgumentNullException("tweet");

            if (user.TweetTwitterProperty == null) return false;
            return (tweet.twitter == user.id && tweet.UserFromProperty != null);
        }



        /// <summary>
        /// Obtient une image source à partir d'un avatar (bytes).
        /// </summary>
        /// <param name="avatar"></param>
        /// <returns></returns>
        public static ImageSource GetImageSourceFromAvatar(byte[] avatar)
        {
            return avatar == null
                       ? ImageLoader.GetDefaultImageSource()
                       : ImageLoader.GetImageSourceFromBytes(avatar);
        }

        /// <summary>
        /// Obtient un avatar à partir d'un ImageSource.
        /// </summary>
        /// <param name="imageSource"></param>
        /// <returns></returns>
        public static byte[] GetAvatarFromImageSource(ImageSource imageSource)
        {
            if (imageSource != null)
                return ImageLoader.GetBytesFromImageSource(imageSource);
            return null;
        }

        public static bool IsCurrentUserOwner(Tweet tweet)
        {
            if (tweet == null) throw new ArgumentNullException("tweet");
            if (LOGGED_USER == null) return false;
            return IsUserOwner(LOGGED_USER, tweet);
        }

        public static bool IsUserOwner(User user, Tweet tweet)
        {
            return user.id == tweet.twitter;
        }

        public static bool CanReTweet(User user, Tweet tweet)
        {
            return tweet.twitter != user.id;
        }

        public static bool CanUndoReTweet(User user, Tweet tweet)
        {
            return tweet.twitter == user.id && tweet.UserFromProperty != null;
        }
    }
}
